package com.college.controller.products;

import com.college.common.constant.ErrorMsg;
import com.college.common.pojo.ao.Favorite;
import com.college.common.pojo.vo.ResultVo;
import com.college.common.utils.UserUtils;
import com.college.products.service.FavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

@RestController
@RequestMapping("/favorite")
@Api(tags = "收藏")
@Slf4j
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    @ApiOperation(value = "新增收藏")
    @PostMapping("/add")
    public ResultVo addFavorite(@RequestBody Favorite favorite){
        Long shUserId = UserUtils.getHeaderUserId();
        favorite.setUserId(shUserId);
        favorite.setCreateTime(new Date());
        if(favoriteService.addFavorite(favorite)){
            return ResultVo.success(favorite.getId());
        }
        return ResultVo.fail(ErrorMsg.FAVORITE_EXIT);
    }

    @ApiOperation(value = "删除收藏")
    @GetMapping("/delete")
    public ResultVo deleteFavorite(@RequestParam Long id){
        if(favoriteService.deleteFavorite(id)){
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }

    @ApiOperation(value = "判断是否收藏")
    @GetMapping("/check")
    public ResultVo checkFavorite(@RequestParam Long idleId){
        Long shUserId = UserUtils.getHeaderUserId();
        return ResultVo.success(favoriteService.isFavorite(shUserId,idleId));
    }

    @ApiOperation(value = "查收藏列表")
    @GetMapping("/my")
    public ResultVo getMyFavorite(){
        Long shUserId = UserUtils.getHeaderUserId();
        return ResultVo.success(favoriteService.getAllFavorite(shUserId));
    }
}
